package com.qifan.alb2b.service;

import org.springframework.stereotype.Service;

import com.qifan.alb2b.dao.CompanyInfoDao;
import com.qifan.alb2b.dao.UserInfoDao;
import com.qifan.alb2b.domain.CompanyInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

@Service
public class CompanyInfoService {
	private CompanyInfoDao companyInfoDao;
	private UserInfoDao userInfoDao;

	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	public void setCompanyInfoDao(CompanyInfoDao companyInfoDao) {
		this.companyInfoDao = companyInfoDao;
	}
	
	public Page query(DataGridModel dgm,CompanyInfoDomain uqp){
		return companyInfoDao.query(dgm,uqp);
	}
	public void getUserByCompId(String [] ids){
		for(int i=0;i<ids.length;i++){
			UserInfoDomain userInfo=userInfoDao.getUserByCompId(ids[i]);
			if(userInfo!=null){
				throw new BusinessException("id为"+ids[i]+"的公司有下属用户，请先删除用户后再删除公司！");
			}
		}
	}
	public CompanyInfoDomain getCompByName(String name){
		CompanyInfoDomain companyInfoDomain =companyInfoDao.getCompanyByName(name);
		if(companyInfoDomain==null){
			throw new BusinessException("公司名为["+name+"]的公司不存在！");	
		}else{
			return companyInfoDomain;
		}
	}
	public CompanyInfoDomain getCompById(String id){
		CompanyInfoDomain companyInfoDomain =companyInfoDao.getCompanyById(id);
		if(companyInfoDomain==null){
			throw new BusinessException("公司ID为["+id+"]的公司不存在！");	
		}else{
			return companyInfoDomain;
		}
	}
	public void add(CompanyInfoDomain companyInfoDomain){
		companyInfoDao.add(companyInfoDomain);
	}
	
	public void update(CompanyInfoDomain companyInfoDomain){
		companyInfoDao.update(companyInfoDomain);
	}
	
	public void delete(String[] ids){
		companyInfoDao.delete(ids);
	}
}
